<?php
class ControllerRestaurantAutocomplete extends Controller {	

	public function template() {
		$this->load->model('setting/template');
		
		$template = $this->model_setting_template->getTemplate($this->request->get['template_id']);
		
		if (isset($template) && file_exists(DIR_IMAGE . 'templates/' . $template['directory'] . '.png')) {
			$image = HTTPS_IMAGE . 'templates/' . $template['directory'] . '.png';
		} else {
			$image = HTTPS_IMAGE . 'no_image.jpg';
		}
		
		$this->response->setOutput('<img src="' . $image . '" alt="" title="" style="border: 1px solid #EEEEEE;" />');
	}		
	
	public function restaurant() {
		$json = array();
		
		if (isset($this->request->get['filter_name'])) {
			$this->load->model('restaurant/restaurant');
			
			$data = array(
				'filter_name' => $this->request->get['filter_name'],
				'ignore_restaurant_ids'    => isset($this->request->get['ignore_restaurant_ids'])?$this->request->get['ignore_restaurant_ids']:null,
				'start'       => 0,
				'limit'       => 20
			);
			
			$restaurants = $this->model_restaurant_restaurant->getRestaurantsForAutocomplete($data);
			
			foreach ($restaurants as $restaurant) {
				$json[] = array(
					'restaurant_id'    => $restaurant['restaurant_id'],
					'name'         => html_entity_decode($restaurant['name'], ENT_QUOTES, 'UTF-8'),
					'url'         => html_entity_decode($restaurant['url'], ENT_QUOTES, 'UTF-8')
				);
			}
		}

		$sort_order = array();
		
		foreach ($json as $key => $value) {
			$sort_order[$key] = $value['name'];
		}

		array_multisort($sort_order, SORT_ASC, $json);
				
		$this->response->setOutput(json_encode($json));
	}
	
	public function restaurantOption() {
		$json = array();
		
		if (isset($this->request->get['filter_name'])) {
			$this->load->language('restaurant/restaurant_option');
			
			$this->load->model('restaurant/option');
			
			$this->load->model('setting/image');
			
			$data = array(
				'group'       => 'restaurant',
				'filter_name' => $this->request->get['filter_name'],
				'start'       => 0,
				'limit'       => 20
			);
			
			$options = $this->model_restaurant_option->getOptions($data);
			
			foreach ($options as $option) {
				$option_value_data = array();
				
				if ($option['type'] == 'select' || $option['type'] == 'radio' || $option['type'] == 'checkbox' || $option['type'] == 'image') {
					$option_values = $this->model_restaurant_option->getOptionValues($option['option_id']);
					
					foreach ($option_values as $option_value) {
						if ($option_value['image'] && file_exists(DIR_IMAGE . $option_value['image'])) {
							$image = $this->model_setting_image->resize($option_value['image'], 50, 50);
						} else {
							$image = '';
						}
													
						$option_value_data[] = array(
							'option_value_id' => $option_value['option_value_id'],
							'name'            => html_entity_decode($option_value['name'], ENT_QUOTES, 'UTF-8'),
							'image'           => $image					
						);
					}
					
					$sort_order = array();
				  
					foreach ($option_value_data as $key => $value) {
						$sort_order[$key] = $value['name'];
					}
			
					array_multisort($sort_order, SORT_ASC, $option_value_data);					
				}
				
				$type = '';
				
				if ($option['type'] == 'select' || $option['type'] == 'radio' || $option['type'] == 'checkbox' || $option['type'] == 'image') {
					$type = $this->language->get('text_choose');
				}
				
				if ($option['type'] == 'text' || $option['type'] == 'textarea') {
					$type = $this->language->get('text_input');
				}
				
				if ($option['type'] == 'file') {
					$type = $this->language->get('text_file');
				}
				
				if ($option['type'] == 'date' || $option['type'] == 'datetime' || $option['type'] == 'time') {
					$type = $this->language->get('text_date');
				}
												
				$json[] = array(
					'option_id'    => $option['option_id'],
					'name'         => html_entity_decode($option['name'], ENT_QUOTES, 'UTF-8'),
					'category'     => $type,
					'type'         => $option['type'],
					'option_value' => $option_value_data
				);
			}
		}

		$sort_order = array();
	  
		foreach ($json as $key => $value) {
			$sort_order[$key] = $value['name'];
		}

		array_multisort($sort_order, SORT_ASC, $json);
				
		$this->response->setOutput(json_encode($json));
	}
	
	public function city() {
		$output = '';
		
		$this->load->model('setting/city');
		
		$results = $this->model_setting_city->getCitiesByCountryId($this->request->get['country_id']);
		
		foreach ($results as $result) {
			$output .= '<option value="' . $result['city_id'] . '"';

			if (isset($this->request->get['city_id']) && ($this->request->get['city_id'] == $result['city_id'])) {
				$output .= ' selected="selected"';
			}

			$output .= '>' . $result['name'] . '</option>';
		}

		if (!$results) {
			$output .= '<option value="0">' . $this->language->get('text_none') . '</option>';
		}

		$this->response->setOutput($output);
	}

	public function district() {
		$output = '';
		
		$this->load->model('setting/city');
		
		$results = $this->model_setting_city->getDistrictsByCityId($this->request->get['city_id']);
		
		foreach ($results as $result) {
			$output .= '<option value="' . $result['district_id'] . '"';

			if (isset($this->request->get['district_id']) && ($this->request->get['district_id'] == $result['district_id'])) {
				$output .= ' selected="selected"';
			}

			$output .= '>' . $result['name'] . '</option>';
		}

		if (!$results) {
			$output .= '<option value="0">' . $this->language->get('text_none') . '</option>';
		}

		$this->response->setOutput($output);
	}
}
?>